/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esper.common.internal.epl.join.querygraph;

import com.espertech.esper.common.client.EventType;
import com.espertech.esper.common.client.scopetest.EPAssertionUtil;
import com.espertech.esper.common.client.type.EPTypePremade;
import com.espertech.esper.common.internal.epl.expression.core.ExprIdentNode;
import com.espertech.esper.common.internal.epl.expression.core.ExprIdentNodeImpl;
import com.espertech.esper.common.internal.supportunit.event.SupportEventTypeFactory;
import com.espertech.esper.common.internal.supportunit.util.SupportQueryGraphTestUtil;
import junit.framework.TestCase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.espertech.esper.common.internal.supportunit.util.SupportExprNodeFactory.makeIdentNode;

public class TestQueryGraphForge extends TestCase {
    private QueryGraphForge queryGraph;
    private EventType[] types;

    public void setUp() {
        queryGraph = new QueryGraphForge(3, null, false);
        types = new EventType[]{
                SupportEventTypeFactory.createMapType(createType("p0,p00,p01,p02")),
                SupportEventTypeFactory.createMapType(createType("p1,p10,p11,p12")),
                SupportEventTypeFactory.createMapType(createType("p2,p20,p21")),
                SupportEventTypeFactory.createMapType(createType("p3,p30,p31")),
                SupportEventTypeFactory.createMapType(createType("p4,p40,p41,p42")),
        };
    }

    public void testFillEquivalency() {
        // test with just 3 streams
        queryGraph.addStrictEquals(0, "p00", make(0, "p00"), 1, "p10", make(1, "p10"));
        queryGraph.addStrictEquals(1, "p10", make(1, "p10"), 2, "p20", make(2, "p20"));

        assertFalse(queryGraph.isNavigableAtAll(0, 2));
        assertEquals(0, SupportQueryGraphTestUtil.getStrictKeyProperties(queryGraph, 0, 2).length);
        assertEquals(0, SupportQueryGraphTestUtil.getIndexProperties(queryGraph, 0, 2).length);

        QueryGraphForge.fillEquivalentNav(types, queryGraph);

        assertTrue(queryGraph.isNavigableAtAll(0, 2));
        String[] expectedOne = new String[]{"p00"};
        String[] expectedTwo = new String[]{"p20"};
        assertTrue(Arrays.equals(expectedOne, SupportQueryGraphTestUtil.getStrictKeyProperties(queryGraph, 0, 2)));
        assertTrue(Arrays.equals(expectedTwo, SupportQueryGraphTestUtil.getIndexProperties(queryGraph, 0, 2)));

        // test with 5 streams, connect all streams to all streams
        queryGraph = new QueryGraphForge(5, null, false);
        queryGraph.addStrictEquals(0, "p0", make(0, "p0"), 1, "p1", make(1, "p1"));
        queryGraph.addStrictEquals(3, "p3", make(3, "p3"), 4, "p4", make(4, "p4"));
        queryGraph.addStrictEquals(2, "p2", make(2, "p2"), 3, "p3", make(3, "p3"));
        queryGraph.addStrictEquals(1, "p1", make(1, "p1"), 2, "p2", make(2, "p2"));

        QueryGraphForge.fillEquivalentNav(types, queryGraph);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i == j) {
                    continue;
                }
                assertTrue("Not navigable: i=" + i + " j=" + j, queryGraph.isNavigableAtAll(i, j));
            }
        }
    }

    public void testAdd() {
        // Try invalid add
        try {
            queryGraph.addStrictEquals(1, null, null, 2, null, null);
            fail();
        } catch (IllegalArgumentException ex) {
            // expected
        }

        // Try invalid add
        try {
            queryGraph.addStrictEquals(1, "a", null, 1, "b", null);
            fail();
        } catch (IllegalArgumentException ex) {
            // expected
        }

        // Try :        s1.p11 = s2.p21  and  s2.p22 = s3.p31
        assertTrue(queryGraph.addStrictEquals(1, "p11", make(1, "p11"), 2, "p21", make(2, "p21")));

        try {
            queryGraph.addStrictEquals(2, "p22", null, 3, "p31", null);
            fail();
        } catch (IllegalArgumentException ex) {
            // success
        }

        try {
            queryGraph.addStrictEquals(2, "p22", null, 3, "p31", null);
            fail();
        } catch (IllegalArgumentException ex) {
            // success
        }

        log.debug(queryGraph.toString());
    }

    public void testIsNavigable() {
        ExprIdentNode fake = makeIdentNode("theString", "s0");

        assertFalse(queryGraph.isNavigableAtAll(0, 1));
        assertFalse(queryGraph.isNavigableAtAll(0, 2));
        assertFalse(queryGraph.isNavigableAtAll(1, 2));

        queryGraph.addStrictEquals(0, "p1", fake, 1, "p2", fake);
        assertTrue(queryGraph.isNavigableAtAll(0, 1));
        assertFalse(queryGraph.isNavigableAtAll(0, 2));
        assertFalse(queryGraph.isNavigableAtAll(1, 2));

        queryGraph.addStrictEquals(2, "p1", fake, 1, "p2", fake);
        assertTrue(queryGraph.isNavigableAtAll(0, 1));
        assertFalse(queryGraph.isNavigableAtAll(0, 2));
        assertTrue(queryGraph.isNavigableAtAll(1, 2));

        queryGraph.addStrictEquals(2, "p1", fake, 0, "p2", fake);
        assertTrue(queryGraph.isNavigableAtAll(0, 1));
        assertTrue(queryGraph.isNavigableAtAll(0, 2));
        assertTrue(queryGraph.isNavigableAtAll(1, 2));
    }

    public void testGetNavigableStreams() {
        ExprIdentNode fake = makeIdentNode("theString", "s0");

        queryGraph = new QueryGraphForge(5, null, false);
        queryGraph.addStrictEquals(3, "p3", fake, 4, "p4", fake);
        queryGraph.addStrictEquals(2, "p2", fake, 3, "p3", fake);
        queryGraph.addStrictEquals(1, "p1", fake, 2, "p2", fake);

        assertEquals(0, queryGraph.getNavigableStreams(0).size());
        EPAssertionUtil.assertEqualsAnyOrder(new int[]{2}, queryGraph.getNavigableStreams(1));
        EPAssertionUtil.assertEqualsAnyOrder(new int[]{1, 3}, queryGraph.getNavigableStreams(2));
        EPAssertionUtil.assertEqualsAnyOrder(new int[]{2, 4}, queryGraph.getNavigableStreams(3));
        EPAssertionUtil.assertEqualsAnyOrder(new int[]{3}, queryGraph.getNavigableStreams(4));
    }

    public void testGetProperties() {
        // s1.p11 = s0.p01 and s0.p02 = s1.p12
        queryGraph.addStrictEquals(1, "p11", make(1, "p11"), 0, "p01", make(0, "p01"));
        queryGraph.addStrictEquals(0, "p02", make(0, "p02"), 1, "p12", make(1, "p12"));
        log.debug(queryGraph.toString());

        String[] expectedOne = new String[]{"p11", "p12"};
        String[] expectedTwo = new String[]{"p01", "p02"};
        assertTrue(Arrays.equals(expectedTwo, SupportQueryGraphTestUtil.getIndexProperties(queryGraph, 1, 0)));
        assertTrue(Arrays.equals(expectedOne, SupportQueryGraphTestUtil.getIndexProperties(queryGraph, 0, 1)));
        assertTrue(Arrays.equals(expectedOne, SupportQueryGraphTestUtil.getStrictKeyProperties(queryGraph, 1, 0)));
        assertTrue(Arrays.equals(expectedTwo, SupportQueryGraphTestUtil.getStrictKeyProperties(queryGraph, 0, 1)));
    }

    private Map<String, Object> createType(String propCSV) {
        String[] props = propCSV.split(",");
        Map<String, Object> type = new HashMap<String, Object>();
        for (int i = 0; i < props.length; i++) {
            type.put(props[i], EPTypePremade.STRING.getEPType());
        }
        return type;
    }

    private ExprIdentNode make(int stream, String p) {
        return new ExprIdentNodeImpl(types[stream], p, stream);
    }

    private final static Logger log = LoggerFactory.getLogger(TestQueryGraphForge.class);
}
